window.K=window.K||{"URL":{"defaultTemplates":""}};
window._jstemplates=window._jstemplates||{};
K=$.extend(K,{
		"BasicForm":{
		"View":null,
		"template":{"name":"default","url":"template.html"},
		"installation":function(){
			K.BasicForm.View=Backbone.View.extend({
			    "tagName":"div",
			    "className":"basicform-wrapper",
			    "template":_.template($("#_template>#t_basicform").text()),
			    "dataURL":false,
			    "data":false,
			    "form":null,
			    "events":{"onButtonClick":{"dft":function(form,btn){}}},
			    "wrapper":null,
			    "setForm":function(form){
			    	this.form=$.extend({
			    		"action":"",
						"method":"post",
			    		"title":"",
						"subtitle":"",
						"items":[]
			    	},form);
			    	return this;
			    },
			    "setButtons":function(btns){
			    	if(this.$el&&this.$el.find(".buttons").length>0){
			    		this.$el.find(".buttons").empty();
			    		for(var i=0;i<btns.length;i++){
			    			var btn=btns[i],$btn=$("<span class='btn bg-c1 font-c4' />");
			    			$btn.text(btn.text);
			    			$btn.attr("data-id",btn.id);
			    			if(btn.fontColor){$btn.css("color",btn.fontColor);}
			    			if(btn.bgColor){$btn.css("background-color",btn.bgColor);}
			    			this.$el.find(".buttons").append($btn);
			    			$btn.click(function(_this){
			    				return function(){
			    					var _id=$(this).attr("data-id"),_evt;
			    					if(typeof(_this.events.onButtonClick[_id])=="function"){
						    			_evt=_this.events.onButtonClick[_id];
						    		}else{
						    			_evt=_this.events.onButtonClick["_dft"];
						    		}
			    					_evt(_this,$(this));
			    				};
			    			}(this));
			    		}
			    	}
			    	return this;
			    },
			    "setData":function(data_url){
			    	if(typeof(data_url)=="string")this.dataURL=data_url;
			    	else{this.data=data_url;}
			    	return this;
			    },"setURL":function(url){
			    	this.dataURL=url;
			    	return this;
			    },
			    "setTitle":function(str){
			    	this.form.title=str;
			    	this.$el.find(".form-title").text(str);
			    	return this;
			    },
			    "setSubtitle":function(str){
			    	this.form.subtitle=str;
			    	this.$el.find(".form-subtitle").text(str);
			    	return this;
			    },
			    "fillData":function(obj_url){
			    	if(obj_url){
			    		if(typeof(obj_url)=="string"){
			    			$.ajax({
					    		"url":obj_url,
					    		"type":"get",
					    		"dataType":"json",
					    		"_this":this,
					    		"success":function(obj){
					    			this._this.setFormData(obj);
					    		},
					    		"error":function(e1,e2,e3){
					    			K.Tool.log(e2);
					    		}
					    	});
			    		}else{
			    			this.setFormData(obj_url);
			    		}
			    	}else if(this.data){
			    		this.setFormData(this.data);
			    	}else if(this.dataURL){
			    		$.ajax({
				    		"url":this.dataURL,
				    		"type":"get",
				    		"dataType":"json",
				    		"_this":this,
				    		"success":function(obj){
				    			this._this.setFormData(obj);
				    		},
				    		"error":function(e1,e2,e3){
				    			K.Tool.log(e2);
				    		}
				    	});
			    	}
			    	return this;
			    },
			    "setAction":function(url){
			    	var $f=this.$el.find("form");
			    	if($f.length>0){
			    		$f.attr("action",url);
			    	}
			    	return this;
			    },
			    "setFormData":function(obj){
			    	var $f=this.$el.find("form");
			    	for(var k in obj){
			    		var $i=$f.find("input[name='"+k+"']");
			    		if($i.attr("type")!="radio"){
			    			$i.val(obj[k]);
			    		}else{
			    			$i.find("value='"+obj[k]+"'").click();
			    		}
			    	}
			    	return this;
			    },
			    "show":function(){
			    	this.$el.show();
			    	return this;
			    },
			    "hide":function(){
			    	this.$el.hide();
			    	return this;
			    },
			    "getForm":function(){
			    	return this.$el.find("form:first");
			    },
			    "empty":function(){
			    	this.$el.find("input").each(function(){
			    		var $i=$(this);
			    		if($i.attr("type")!="radio"){
			    			$i.val("");
			    		}
			    	});
			    	this.activate();
			    	return this;
			    },
			    "to":function($dom){
			    	if(this.form){
				    	this.$el.empty();
				    	this.events=$.extend({},this.events);
				    	this.render(this.form);
				    	$dom.append(this.$el);
				    	this.$el.height($dom.height());
				    	this.activate();
			    	}else{
			    		K.Tool.log("No formset error.");
			    	}
			    	return this;
			    },
			    "activate":function(){
			    	var $radio=this.$el.find("input[type='radio']");
			    	for(var i=$radio.length-1;i>=0;i--){
			    		$radio.eq(i).click();
			    	}
			    	return this;
			    },
			    "buttonClick":function(id,fn){
			    	this.events.onButtonClick[id]=function(){};
			    	if(typeof(fn)=="function"){this.events.onButtonClick[id]=fn;}
			    	return this;
			    },
			    "render":function(json){  
			        this.$el.html(this.template(json));
			        return this.$el;
			     }
			});
		},"init":function($dom){
			$dom.empty();
			var form=new K.BasicForm.View();
			form.to($dom);
			return form;
		}	
	}
});


$.extend(_jstemplates,{"BasicForm":function(cb){
	if($("#_template").length<=0){
		$("body").append($("<div type='text/template' id='_template' />"));
	}
	var arr=[];
	arr.push('<script id="t_basicform" type="text/template">');
	arr.push('<h3 class="form-title font-c3">{-title}</h3>');
	arr.push('<p class="form-subtitle">{-subtitle}</p>');
	arr.push('<form class="bg-c3" action="{-action}" method="{-method}" >');
	arr.push('{for(var i=0;i<items.length;i++){var item=items[i];}');
	arr.push('{if(item.type!="hidden"){}');
	arr.push('<label class="font-c2">{-item.text}<a class="error"></a></label>');
	arr.push('{}}');
	arr.push('{if(item.type&&item.type.toLowerCase()=="password"){}')
	arr.push('<div class="form-item ">');
	arr.push('<input type="password" class="font-c2" name="{-item.key}" placeholder="{-(item.tip||item.text)}" />');
	arr.push('</div>');
	arr.push('{}else if(item.type&&item.type.toLowerCase()=="radio"){}');
	arr.push('{if(item.option){}');
	arr.push('<div class="form-item-noboder ">');
	arr.push('{for(var k in item.option){}');
	arr.push('<input class="form-radio" type="radio" value="{-k}" name="{-item.key}" /><a class="font-c2">{-item.option[k]}</a>');
	arr.push('{}}');
	arr.push('</div>');
	arr.push('{}}');
	arr.push('{}else if(item.type&&item.type.toLowerCase()=="hidden"){}');
	arr.push('<input type="hidden" name="{-item.key}" value="" />');
	arr.push('{}else{}');
	arr.push('<div class="form-item ">');
	arr.push('<input type="text" class="font-c2" name="{-item.key}" placeholder="{-(item.tip||item.text)}" />');
	arr.push('</div>');
	arr.push('{}}');
	arr.push('{}}');
	arr.push('<div class="bottom-blk bg-c3" />');
	arr.push('<div class="buttons bg-c3" />');
	arr.push('</form>');
	arr.push('</script>');
	if($("#_template").find("#t_basicform").length<=0)
	$("#_template").append(arr.join(""));
	if(typeof(cb)=="function")cb();
}});